<?php

require_once 'data_layer.php';

//functions
// ===============================
function checkBrowser() {
    $chrome = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
    if (!$chrome) {
        die("<script>alert('You must use Google Chrome.')</script>");
    }
}

// ===============================
function getClientIP() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if ($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if ($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if ($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if ($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if ($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
}

// ===============================
function createParticipantID() {
    //start session
    if (!isset($_SESSION)) {
        session_start();
    }

    $pid = (isset($_SESSION['pid_prefix']) ? $_SESSION['pid_prefix'] : '') . "P" . mt_rand(100, 9999);
    while (participantExists($pid)) {
        $pid = "P" . mt_rand(100, 9999);
    }

    return $pid;
}

//post function dispatcher
// ===============================
if (isset($_POST["function_name"])) {
    $function_name = $_POST["function_name"];

    //start the session
    if (!isset($_SESSION)) {
        session_start();
    }

//    //setRecipeNumber()
//    if ($function_name == "setRecipeNumber") {
//        setRecipeNumber($_POST["recipe_number"]);
//    }
}

